Skip to main content

ls.cpp

Source: src/modules/ls.cpp

The ls module provides standard directory listing capabilities. Unlike other modules that rely strictly on WinAPI, this implementation utilizes the C++ Standard Library (std::filesystem) for a modern approach to this. (The WinAPI ways of imitating ls are really dumb)

Function Signature

ModuleResult ls(std::string path);

Parameters

  • path: The absolute or relative path of the directory to list (e.g., C:\Users\ or .).

Logic & Behavior

  1. Validation: Checks if the target path exists and is essentially a directory using fs::exists and fs::is_directory.
  2. Iteration: Loops through the directory contents using a standard iterator.
  3. Formatting:
  • Appends the filename to the output string.
  • If the entry is a directory, it appends a trailing forward slash (/) to visually distinguish it from files.
  • Each entry is followed by a newline \n.

Return Values (ModuleResult)

  • data: A newline-separated string of filenames.
  • windows_error_code:
    • ERROR_SUCCESS (0): Operation completed successfully.
    • ERROR_PATH_NOT_FOUND (3): If the path does not exist or is not a directory.